<template>
  <div>
    <div>
      <el-input
              size="small"
              class="addPosInput"
              placeholder="添加职位..."
              prefix-icon="el-icon-plus"
              v-model="pos.name"
      >
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addPosition()">添加</el-button>
    </div>
    <div>
      <el-table
              v-loading="loading"
              :data="position"
              stripe
              border
              @selection-change="handleSelectionChange"
              style="width: 70%">
        <el-table-column
                type="selection" width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="180">
        </el-table-column>
        <el-table-column
                prop="name"
                label="职位名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="createDate"
                label="创建时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    @click="showEditPosition(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                    size="mini"
                    type="danger"
                    @click="deletePosition(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="danger" style="margin-top: 8px" size="small"
                 :disabled="multipleSelection.length==0" @click="deleteMany">
        批量删除
      </el-button>
    </div>
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
      <div>
        <div>
          <el-tag>职位名称</el-tag>
          <el-input class="updatePosInput" size="small" v-model=updatePos.name>

          </el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doUpdate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "DepMana",
    data() {
      return {
        // 列表展示数据
        position: [
          {
            id: '1',
            name: '技术总监',
            createDate: '1111'
          }
        ],
        // 添加操作数据
        pos: {
          name: ''
        },
        updatePos: {
          name: ''
        },
        loading: false,
        dialogVisible: false,
        multipleSelection:[]
      }
    },
    methods: {
      // 初始化职位数据列表
      initPosition() {
        this.loading = true;
        this.getRequest('/system/basic/pos/').then(resp => {
          this.loading = false;
          if (resp) {
            this.position = resp;
          }
        })
      },
      // 添加职位
      addPosition() {
        this.postRequest('/system/basic/pos/', this.pos).then(resp => {
          this.initPosition();
          this.pos.name = '';
        })
      },
      // 删除职位
      deletePosition(index, data) {
        this.$confirm('此操作将永久删除【 '+ data.name +' 】职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
            this.initPosition();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 显示编辑对话框
      showEditPosition(index, data) {
        // 将选择编辑的数据复制到 updataPos 以便编辑
        Object.assign(this.updatePos, data);
        // 设置对话框为显示状态
        this.dialogVisible = true;
      },
      // 更新职位
      doUpdate() {
        this.putRequest('/system/basic/pos/', this.updatePos).then(resp => {
          if (resp) {
            this.initPosition();
            this.updatePos.name = '';
            this.dialogVisible = false;
          }
        })
      },
      // 获取多选数据
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 批量删除
      deleteMany() {
        this.$confirm('此操作将批量删除【'+ this.multipleSelection.length +'】个职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?';
          this.multipleSelection.forEach(item => {
            ids += 'ids=' +item.id + '&';
          })
          this.deleteRequest('/system/basic/pos/'+ ids).then(resp => {
            if (resp){
              this.initPosition();
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});
      }

    },
    mounted() {
      this.initPosition();
    }
  }
</script>

<style>

  .addPosInput {
    width: 300px;
    margin-right: 8px;
  }

  .updatePosInput {
    width: 300px;
    margin-left: 8px;
  }
</style>